package uk.ac.ebi.microarray.expressionprofiler.ra.fs;

import javax.resource.spi.ConnectionRequestInfo;

import org.apache.commons.lang.builder.EqualsBuilder;

/**
 * <p><code>FileSystemConnectionInfo</code> class defines connection properties.</p>
 * @author Larionov, Konstantin
 *
 */
public class FileSystemConnectionInfo implements ConnectionRequestInfo {

    private String dir;
    private boolean isDirAbsolute;
    private boolean isUniqueIdentifierNecessary;

    /**
     * <p>Instantiates new <code>FileSystemConnectionInfo</code> class.</p>
     * <p>By default it is assumed that path to file system consists of three parts:</p>
     * <p>1. RepositoryPath - path to local file system repository defined in AE_EP Connector datasource,
     * <br>2. Directory Prefix - set by <code>dir</code> parameter and
     * <br>3. Directory Suffix - generated automatically and concatenated with Directory Prefix.</p>
     * <p>That is: "%Repository Path%/dirPrefix.%Suffix%".</p>
     * <p>Directory Suffix is not generated and concatenated in case if <code>absolute</code> parameter is set to <code>true</code>.</p>
     * @param dir Directory Prefix (in case if <code>absolute = false</code>) or absolute file system path to local file system directory (in case if <code>absolute = true</code>).
     * @param absolute Flag indicating whether absolute path is set in <code>dir</code> or not.
     * @see uk.ac.ebi.microarray.expressionprofiler.ra.fs.FileSystemConnectionInfo#FileSystemConnectionInfo(String, boolean, boolean)
     */
    public FileSystemConnectionInfo(String dir, boolean absolute) {
        this.dir = dir;
        this.isDirAbsolute = absolute;
        this.isUniqueIdentifierNecessary = true;
    }

    /**
     * <p>Instantiates new <code>FileSystemConnectionInfo</code> class.</p>
     * <p>By default it is assumed that path to file system consists of three parts:</p>
     * <p>1. RepositoryPath - path to local file system repository defined in AE_EP Connector datasource,
     * <br>2. Directory Prefix - set by <code>dir</code> parameter and
     * <br>3. Directory Suffix - generated automatically and concatenated with Directory Prefix.</p>
     * <p>That is: "%Repository Path%/dirPrefix.%Suffix%".</p>
     * <p>Directory Suffix is not generated and concatenated in case if <code>absolute</code> parameter is set to <code>true</code>.</p>
     * @param dir Directory Prefix (in case if <code>absolute = false</code>) or absolute file system path to local file system directory (in case if <code>absolute = true</code>).
     * @param absolute Flag indicating whether absolute path is set in <code>dir</code> or not.
     * @param isUniqueIdentifierNecessary Flag indicating whether unique Directory Suffix should be added to <code>dir</code>.
     * @see uk.ac.ebi.microarray.expressionprofiler.ra.fs.FileSystemConnectionInfo#FileSystemConnectionInfo(String, boolean)
     */
    public FileSystemConnectionInfo(String dir, boolean absolute, boolean isUniqueIdentifierNecessary) {
        this.dir = dir;
        this.isDirAbsolute = absolute;
        this.isUniqueIdentifierNecessary = isUniqueIdentifierNecessary;
    }

    public String getDir() {
        return dir;
    }

    public boolean isDirAbsolute() {
        return isDirAbsolute;
    }

    public boolean isUniqueIdentifierNecessary() {
        return isUniqueIdentifierNecessary;
    }

    @Override
    public boolean equals(Object object) {
        if (!isDirAbsolute) {
            return false;
        }
        if (!(object instanceof FileSystemConnectionInfo)) {
            return false;
        }
        FileSystemConnectionInfo rhs = (FileSystemConnectionInfo) object;
        return new EqualsBuilder().append(this.dir, rhs.dir).append(this.isDirAbsolute, rhs.isDirAbsolute).append(
                this.isUniqueIdentifierNecessary, rhs.isUniqueIdentifierNecessary).isEquals();
    }

}
